package com.halflife.framework.participant.range.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.halflife.framework.participant.Teacher;
import com.halflife.framework.participant.TreeView;
import com.halflife.framework.participant.user.UserView;
import com.halflife.framework.participant.user.UserViewRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RangeService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private UserViewRepository userViewRepository;

    public String getJsonByOper(Integer roleId, Integer oper) throws Exception{
        return getTeacherJson();
    }

    public String getTeacherJson() throws Exception{

        //输出结果字符串
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        //设置参数
        Map<String, Object> param = new HashMap<>();
        //获取父权限
        param.put("valid", true);
        List<UserView> userViewList = userViewRepository.selectAll(param);
        if(userViewList.size() > 0){

            List<TreeView> gradeJson = new ArrayList<>();
            for(UserView school: userViewList){

                if(school != null){

                    if(sb.length() > 3){
                        sb.append(",");
                    }
                    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
                    //转化学校JSON
                    String str = gson.toJson(new TreeView(school.getId(), school.getUserName()));
                    sb.append(str);
                }
            }
        }
        sb.append("]");
        return sb.toString();
    }
}
